package com.zhong.bmob.server;

import android.os.Environment;

import com.zhong.bmob.inter.IFileDownLoadDown;
import com.zhong.bmob.model.FileSavePath;
import com.zhong.bmob.model.User;

import java.io.File;
import java.io.IOException;

import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.DownloadFileListener;

/**
 * Created by 俊峰 on 2017/5/16.
 * 专门用于将公钥文件从Bmob上下载下来
 */

public class FileDownLoad {
    public static void doFileDownLoad(User user, IFileDownLoadDown fileDownLoadDown){
        final IFileDownLoadDown iFileDownLoadDown = fileDownLoadDown;
        BmobFile file = new BmobFile("publicKey.txt", "", user.getPublicKeyFile());
        File fileSaveTo = new File(Environment.getExternalStorageDirectory(), FileSavePath.theFriendPublicKey);
        if (!fileSaveTo.exists()) {
            if (fileSaveTo.mkdir()) {
                File theOtherSave = new File(fileSaveTo, user.getUsername());
                if (!theOtherSave.exists()) {
                    if (theOtherSave.mkdir()) {
                        File saveTheKey = new File(theOtherSave, "publicKey.txt");
                        if (!saveTheKey.exists()) {
                            try {
                                if (saveTheKey.createNewFile()) {
                                    file.download(saveTheKey, new DownloadFileListener() {
                                        @Override
                                        public void done(String s, BmobException e) {
                                            if (e == null){
                                                iFileDownLoadDown.fileDownSuccess(s);
                                            }else {
                                                iFileDownLoadDown.fileDownFail(e);
                                            }
                                        }

                                        @Override
                                        public void onProgress(Integer integer, long l) {

                                        }
                                    });
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }else {
            File theOtherSave = new File(fileSaveTo, user.getUsername());
            if (!theOtherSave.exists()) {
                if (theOtherSave.mkdir()) {
                    File saveTheKey = new File(theOtherSave, "publicKey.txt");
                    if (!saveTheKey.exists()) {
                        try {
                            if (saveTheKey.createNewFile()) {
                                file.download(saveTheKey, new DownloadFileListener() {
                                    @Override
                                    public void done(String s, BmobException e) {
                                        if (e == null){
                                            iFileDownLoadDown.fileDownSuccess(s);
                                        }else {
                                            iFileDownLoadDown.fileDownFail(e);
                                        }
                                    }

                                    @Override
                                    public void onProgress(Integer integer, long l) {

                                    }
                                });
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
